package v1

import (
	"ginblog/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

// 添加文章
func AddArt(c *gin.Context) {
	data := &model.Article{}
	c.ShouldBindJSON(data)
	code = model.CreateArt(data)
	Response(c, code, data)
}

// 查询文章列表
func GetCateArt(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))
	id, _ := strconv.Atoi(c.Param("id"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = -1
	}
	data, code := model.GetCateArt(pageSize, pageNum, id)
	Response(c, code, data)
}

// 查询文章列表
func GetArt(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = -1
	}
	cate, code := model.GetArt(pageSize, pageNum)
	Response(c, code, cate)
}

// 编辑文章
func EditArt(c *gin.Context) {
	var data model.Article
	id, _ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.EditArt(id, &data)
	Response(c, code, "")
}

// 删除文章
func DeleteArt(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	code := model.DeleteArt(id)
	Response(c, code, "")
}

// 查询单个
func GetArtInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	art, code := model.GetArtInfo(id)
	Response(c, code, art)
}
